<extend name="Public:template" />
<block name="page_body">
<!-- 购物车页面  -->
<header class="headerSub clearfix">
    <h2>我的购物车</h2>
    <a href="#" class="backBtn leftBtn" onClick="javascript:window.history.back(-1);return false;">返回上页</a>
    <a href="{:U('Product/index')}" class="yellowNorBg rightBtn">继续购物</a>
</header>
<div class="content clearfix">
	<div class="productList" id="productListView">
		<volist name="cartItems" id="val">
		<dl id="{$val.prod_id}">
			<dt>
				<a href="#" onclick="return false;">
					<img class="lazy"  src="{$val.prod_id|m_prod_pic}"/>
				</a>
			</dt>
			<dd>
				<a href="#">
					<h4>{$val.name} {$val.spec|m_prod_spec=$val['spec_qty'],$val['spec_unit']}</h4>
					<p class="promTags" style="font:red;weight:700;">{$val.stock_message}</p>
					<p class="price">
						<span class="now" id="itemPriceReal{$val.prod_id}" price_real="{$val.price_real}">¥{$val.price_real}</span>
						<span class="old" id="itemPriceSale{$val.prod_id}" price_sale="{$val.price_sale}">¥{$val.price_sale}</span>
					</p>
				</a>
				<p class="cartGroupBtn">
					<a class="sbutton "  href="javascript:addToCart('{$val.prod_id}','-1','1');" >－</a><input type="number" class="cartItemNumInput" id="itemNum{$val.prod_id}" name="itemNum[]" value="{$val.num}" onChange="addToCart('{$val.prod_id}',this.value,'2')"/><a class="sbutton orange" href="javascript:addToCart('{$val.prod_id}','1','1');" >＋</a>
				</p>
			</dd>
		</dl>
		</volist>
	</div>
	<div class="cartBottomContainer">
		<!--统计信息-->
		<div class="cartTotal">
			<p class="alignR" id="suminfo">
				您购买了<span class="deepRed" id="total_item" >{$cartPrice.total_item}</span>种商品，总共<span class="deepRed"  id="total_num">{$cartPrice.total_num}</span>件，总价<span class="deepRed" id="total_amt">¥{$cartPrice.total_amt|number_format=###, 2,'.',''}</span>
	    	</p>
	    	<p class="alignR">
	    		(订单金额超过20元免运费)
	    	</p>
	    	
		</div>
		<if condition="$cartPrice.total_amt gt 0">
			<!--操作按钮-->
			<div class="cartAction">
				<a href="#" class="leftBtn" onClick="doEmptyCart()"><i class="delBtn" ></i>清空购物车</a>
				<a href="{:U('CustOrder/showConfirm')}" class="rightBtn">选择配送地址>></a>
			</div>
		</if>
	
	</div>
</div>
</block>
<block name="script_footer">
<script type="text/javascript">
function addToCart(prod_id,num,edittype){
	//document.location.href = "/cart/doadd.html?prod_id="+prod_id+"&num="+num+"&edittype="+edittype;
	var currNum = parseInt( $("#itemNum"+prod_id).val());
	var leftNum = currNum+parseInt(num);
	if(currNum<=0||(edittype=='1' && leftNum<=0)){
		if(!confirm('确定将该商品从购物车中移除？')){
			return;
		}
	}
	$.ajax({
		'async': true, 
        'url': "/cart/doadd.html",
        'dataType': "json",
        'data': {'prod_id':prod_id,'num':num,'edittype':edittype},
        'success': function (data) {
        	if(data['error']=='0'){
	        	if(data['message']!=''){
	        		alert(data['message']);
	        	}
	        	var cart_num = data['cart_num'];
	        	var item_num = data['item_num'];
	        	$("#cartNum").html(cart_num);
	        	$("#itemNum"+prod_id).val(item_num);
	        	if(item_num<=0){
	        		$("#"+prod_id).remove();
	        	}
	        	if(cart_num<=0){
	        		$(".cartAction").hide();
	        	}
	        	calcPrice();
        	}else{
        		alert(data['message']);
        	}
        }
    });
}
function calcPrice(){
	var totalItem = 0;
	var totalAmt = 0;
	var totalNum = 0;
	$("#productListView dl").each(function(){
		var prod_id = $(this).attr("id");
		var item_num = parseInt($("#itemNum"+prod_id).val());
		var item_price = parseFloat($("#itemPriceReal"+prod_id).attr("price_real"));
		totalAmt += item_price*item_num;
		totalNum += item_num;
		totalItem++;
	});
	$("#total_item").html(totalItem);
	$("#total_num").html(totalNum);
	$("#total_amt").html("¥"+totalAmt.toFixed(2));
	
}
function doEmptyCart(){
	if(confirm('确定清空购物车?')){
		document.location.href="/Cart/doEmpty.html";
	}
}
</script>
</block>